
#include	"SysConfig.h"
#include	"Define.h"
#include	"Function.h"
#include	"Display.h"
#ifdef  ST7565P
#include	"ST7565P.h"
#endif

#ifdef  LC786XXX
#include	"LC786XXX.h"
#endif
#ifdef  MIC
#include	"MIC.h"
#endif

extern UI_INFO_MODE		ucUSB_DisplayInfo;
extern UI_ID3TAG_MODE	ucUSB_Display_ID3Tag;
extern unsigned char	ucUSB_FileName[MEDIA_CHAR_SIZE];
extern unsigned char	ucUSB_FileName_CharCode;
extern unsigned char	ucUSB_FolderName[MEDIA_CHAR_SIZE];
extern unsigned char	ucUSB_FolderName_CharCode;
extern unsigned char	ucUSB_ID3_Title[MEDIA_CHAR_SIZE];
extern unsigned char	ucUSB_ID3_Title_CharCode;
extern unsigned char	ucUSB_ID3_Artist[MEDIA_CHAR_SIZE];
extern unsigned char	ucUSB_ID3_Artist_CharCode;
extern unsigned char	ucUSB_ID3_Album[MEDIA_CHAR_SIZE];
extern unsigned char	ucUSB_ID3_Album_CharCode;

extern unsigned char	ucUSB_Pause;
extern UI_SCAN_MODE		ucUSB_Scan;
extern UI_REPEAT_MODE	ucUSB_Repeat;
extern UI_RANDOM_MODE	ucUSB_Random;
extern UI_PLAY_INFO		sUSB_PlayInfo;
extern unsigned char	ucUSB_FastPlay;

extern unsigned char ucDISP_Blink;
extern unsigned char ucDISP_Buf[12];
extern unsigned char ucDISP_FolderName;
extern unsigned char ucDISP_FileName;
extern unsigned char	ucDISP_FolderName_Timer50msec;
extern unsigned char	ucDISP_FileName_Timer50msec;
extern unsigned char ucDISP_ScrollEnable;
extern unsigned char	ucLC786XXX_USB_Process;

#ifdef  LC786XXX
extern unsigned char	ucLC786XXX_InitializeComplete;
extern LC786X_MEDIA_ID  ucLC786XXX_CurrentMediaID;
extern unsigned char 	ucLC786XXX_ErrorSeq;
extern unsigned short	usLC786XXX_InitUSB_10mecTimer;
extern unsigned char	ucLC786XXX_USB_Process;
#endif

#ifdef  FILE_SEARCH
extern unsigned char	ucUSB_FileSearchName[MEDIA_CHAR_SIZE];
extern unsigned char	ucUSB_FileSearchName_CharCode;
#endif

extern unsigned char USB_GetTextStatus(unsigned char ucDes);

extern unsigned char ucDISP_ClockMode;
extern unsigned char ucDISP_CurrentDisplay_Timer50msec;
extern unsigned char	usDISP_TempDispMode_Timer50msec;
extern UI_DISP ucDISP_TempDispMode;

extern unsigned char DISP_PageScroll(unsigned char ucOpt, UI_INFO_MODE ucMode,unsigned char * ucData,unsigned char ucSize,unsigned char ucUni);
#ifdef  DOT_SCROLL
extern unsigned char DISP_DotScroll(unsigned char ucOpt, UI_INFO_MODE ucMode,unsigned char * ucData,unsigned char ucSize,unsigned char ucUni);
#endif
extern LC786X_FORMAT	ucLC786X_FileFormat_USB;

void USB_DISP_IconMain(void)
{
	if	(ucLC786X_FileFormat_USB == LC786X_FORMAT_WMA)
	{
		DISP_IconSetStatusFlag(1, ICON_MASK_WMA);
	}
	
	if	((ucLC786X_FileFormat_USB == LC786X_FORMAT_MPEG1_LAYER3) 
	||	(ucLC786X_FileFormat_USB == LC786X_FORMAT_MPEG2_LAYER3)
	||	(ucLC786X_FileFormat_USB == LC786X_FORMAT_MPEG25_LAYER3))
	{
		DISP_IconSetStatusFlag(1, ICON_MASK_MP3);
	}

	if  (ucDISP_TempDispMode == DISP_FILE_SEARCH)
	{
		DISP_IconSetStatusFlag(1, ICON_MASK_SCH);
	}

	switch  (ucUSB_Random)
	{
		case RANDOM_ALL:
			DISP_IconSetStatusFlag(1, ICON_MASK_RDM);
			break;
		case RANDOM_FOLDER:
			DISP_IconSetStatusFlag(1, (ICON_MASK_RDM | ICON_MASK_DIR));
			break;
		case RANDOM_NONE:
			break;
	}

	switch  (ucUSB_Repeat)
	{
		case REPEAT_FILE:
			DISP_IconSetStatusFlag(1, ICON_MASK_RPT);
			break;
		case REPEAT_FOLDER:
			DISP_IconSetStatusFlag(1, (ICON_MASK_RPT | ICON_MASK_DIR));
			break;
		case REPEAT_NONE:
			break;			
	}
	

	switch  (ucUSB_Scan)
	{
		case SCAN_ALL:
			DISP_IconSetStatusFlag(1, ICON_MASK_INT);
			break;
		case SCAN_FOLDER:
			DISP_IconSetStatusFlag(1, (ICON_MASK_INT | ICON_MASK_DIR));
		case SCAN_NONE:
			break;
	}
}

void USB_DISP_Display_Er01(void)
{
	unsigned char ucTemp;
	unsigned char ucBuf[11];
	// ucTemp = Need  ErrorNumber Save

	ucBuf[6] = (ucTemp % 10) | '0';
	ucBuf[7] = (ucTemp / 10) | '0';
	ucBuf[8]= 0x00;
	
#ifdef  ST7565P	

	memcpy(&ucBuf[0], "ERROR", 5);

	ST7565_UpdateDisplayData(7, ucBuf[0]);
	ST7565_UpdateDisplayData(16, ucBuf[1]);
	ST7565_UpdateDisplayData(25, ucBuf[2]);
	ST7565_UpdateDisplayData(34, ucBuf[3]);
	ST7565_UpdateDisplayData(44, ucBuf[4]);

	ST7565_UpdateDisplayString(58, &ucBuf[6], 0);
#endif
#ifdef  PT6524
	ucDISP_Buf[5] ='E';
	ucDISP_Buf[6] ='R';

	ucDISP_Buf[9] ='0';
	ucDISP_Buf[10] ='1';
#endif

}
void USB_DISP_FileCheck(void)
{
	unsigned char ucBuf[11];	
		
#ifdef  ST7565P
	memcpy(&ucBuf[0], "FILE CHECK", 10);
	ucBuf[10] = 0x00;

	ST7565_UpdateDisplayData(4, ucBuf[0]);
	ST7565_UpdateDisplayData(12, ucBuf[1]);
	ST7565_UpdateDisplayData(15, ucBuf[2]);
	ST7565_UpdateDisplayData(23, ucBuf[3]);
	ST7565_UpdateDisplayData(37, ucBuf[5]);
	ST7565_UpdateDisplayData(46, ucBuf[6]);
	ST7565_UpdateDisplayData(55, ucBuf[7]);
	ST7565_UpdateDisplayData(64, ucBuf[8]);
	ST7565_UpdateDisplayData(73, ucBuf[9]);
#endif
#ifdef  PT6524
	memcpy(&ucDISP_Buf[3], "FILE CHK ", 9);
#endif
}

void USB_DISP_Pause(void)
{
	unsigned char ucBuf[6];

	if  (ucDISP_Blink == 1)
	{
		return;
	}
#ifdef  ST7565P
	memcpy(&ucBuf[0], "PAUSE", 5);
	ucBuf[5] = 0x00;
	
	ST7565_UpdateDisplayString(14,&ucBuf[0], 0);
#endif
#ifdef  PT6524
	memcpy(&ucDISP_Buf[3], "  PAUSE  ", 9);
#endif

}

void USB_DISP_PlayTime(void)
{
	unsigned char ucBuf[15];
	unsigned short usNumber = 0;	

	memset(&ucBuf[0], ' ', 15);

#ifdef  ST7565P
	usNumber = sUSB_PlayInfo.FileNo;	
	ucBuf[0] = 'T';
	ucBuf[1] = (usNumber / 1000) + '0';
	
	usNumber = (usNumber % 1000);
	ucBuf[2] = (usNumber / 100) + '0';
	
	usNumber = (usNumber % 100);
	ucBuf[3] = (usNumber / 10) + '0';
	
	usNumber = usNumber % 10;
	ucBuf[4] = usNumber + '0';

	ucBuf[5] = ' ';

	ucBuf[6] = (sUSB_PlayInfo.Min / 10) + '0';
	ucBuf[7] = (sUSB_PlayInfo.Min % 10) + '0';

	ucBuf[8] = ':';

	ucBuf[9] = (sUSB_PlayInfo.Sec / 10) + '0';
	ucBuf[10] = (sUSB_PlayInfo.Sec % 10) + '0';
	ucBuf[11] = 0x00;
	
	ST7565_UpdateDisplayString(7, &ucBuf[0], 0);
#endif
#ifdef  PT6524
	ucDISP_Buf[6] = (sUSB_PlayInfo.Min / 10) + '0';
	ucDISP_Buf[7] = (sUSB_PlayInfo.Min % 10) + '0';

	ucDISP_Buf[8] = (sUSB_PlayInfo.Sec / 10) + '0';
	ucDISP_Buf[9] = (sUSB_PlayInfo.Sec % 10) + '0';
	
	DISP_IconSetStatusFlag(1, ICON_MASK_COL);
#endif 
	
}

void USB_DISP_FastF_FastR(void)
{
	USB_DISP_PlayTime();	
}

void USB_DISP_Display_NoInfo(void)
{
	unsigned char ucBuf[10];

	memset(&ucBuf[0], 0x00, 10);
	memcpy(&ucBuf[0], "NO INFO", 7);
			
#ifdef  ST7565P
	ST7565_UpdateDisplayString(14,&ucBuf[0], 0);
#endif
#ifdef  PT6524
	memcpy(&ucDISP_Buf[3], " NO INFO ", 9);
#endif
}

unsigned char USB_DISP_FileName(void)
{
	unsigned char ucRet = 1;

	if  (!USB_GetTextStatus(UI_TEXT_READY_FILENAME))
	{
		return(1);
	}
	
	if  (DISP_PageScroll(ucDISP_FileName, INFO_FILENAME, &ucUSB_FileName[0], MEDIA_CHAR_SIZE, ucUSB_FileName_CharCode) == 1)
	{
		ucDISP_FileName = 0;
		DISP_PageScrollReset();
		ucRet = 0;
	}
	if  (ucDISP_ScrollEnable == 0)
	{
		if  (ucDISP_FileName_Timer50msec == 0)
		{
			ucDISP_FileName = 0;
		}
	}

	return(ucRet);
}

#ifdef  FILE_SEARCH

unsigned char USB_DISP_FileSearch(void)
{
	unsigned char ucRet = 1;
	
	if  (DISP_PageScroll(1, INFO_FILENAME, &ucUSB_FileSearchName[0], MEDIA_CHAR_SIZE, ucUSB_FileSearchName_CharCode) == 1)
	{
		ucRet = 0;		
	}

	if  (ucRet == 0)
	{
		DISP_ClearTempDisplayMode();
		DISP_PageScrollReset();
	}
	
	if  (ucDISP_ScrollEnable == 0)
	{
		if  (ucDISP_FolderName_Timer50msec == 0)
		{
			ucDISP_FolderName = 0;
		}
	}
	return(ucRet);
}

#endif

unsigned char USB_DISP_FolderName(void)
{	
	unsigned char ucRet = 1;
	
	if	(!USB_GetTextStatus(UI_TEXT_READY_FOLDERNAME))
	{
		return(1);
	}
		
	if  (DISP_PageScroll(ucDISP_FolderName, INFO_FOLDERNAME, &ucUSB_FolderName[0], MEDIA_CHAR_SIZE, ucUSB_FolderName_CharCode) == 1)
	{
		ucDISP_FolderName = 0;
		DISP_PageScrollReset();
		ucRet = 0;
	}
	if  (ucDISP_ScrollEnable == 0)
	{
		if  (ucDISP_FolderName_Timer50msec == 0)
		{
			ucDISP_FolderName = 0;
		}
	}

	return(ucRet);
}

void UI_USB_DISP_ID3Tag(void)
{
	unsigned char ucBuf[16];
	unsigned short usBitrate = 0;
	unsigned char ucSize;
	
	memset(&ucBuf[0], 0x00, 16);
		
	switch  (ucUSB_Display_ID3Tag)
	{
		case ID3TAG_ARTIST:
			if  (USB_GetTextStatus(UI_TEXT_READY_ARTIST))
			{
				ucSize = DISP_BufferNull(&ucUSB_ID3_Artist[0], MEDIA_CHAR_SIZE);
				if  (ucSize == 0)
				{
					memcpy(&ucBuf[0], "No Artist", 9);
					DISP_PageScroll(0, INFO_ID3TAG, &ucBuf[0], 9, 0);
				}
				else
				{
					DISP_PageScroll(0, INFO_ID3TAG, &ucUSB_ID3_Artist[0], ucSize + 1, ucUSB_ID3_Artist_CharCode);
				}
			}
			else
			{
				USB_DISP_Display_NoInfo();
			}
			break;
		case ID3TAG_TITLE:
			if  (USB_GetTextStatus(UI_TEXT_READY_TITLE))
			{
				ucSize = DISP_BufferNull(&ucUSB_ID3_Title[0], MEDIA_CHAR_SIZE);
				if  (ucSize == 0)
				{
					memcpy(&ucBuf[0], "No Title", 8);
					DISP_PageScroll(0, INFO_ID3TAG, &ucBuf[0], 8, 0);
				}
				else
				{
					DISP_PageScroll(0, INFO_ID3TAG, &ucUSB_ID3_Title[0], ucSize + 1, ucUSB_ID3_Title_CharCode);
				}
			}
			else
			{
				USB_DISP_Display_NoInfo();
			}
			break;
		case ID3TAG_ALBUM:
			if  (USB_GetTextStatus(UI_TEXT_READY_ALBUM))
			{
				ucSize = DISP_BufferNull(&ucUSB_ID3_Album[0], MEDIA_CHAR_SIZE);
				
				if  (ucSize == 0)
				{
					memcpy(&ucBuf[0], "No Album", 8);
					DISP_PageScroll(0, INFO_ID3TAG, &ucBuf[0], 8, 0);
				}
				else
				{
					DISP_PageScroll(0, INFO_ID3TAG, &ucUSB_ID3_Album[0], ucSize + 1, ucUSB_ID3_Album_CharCode);
				}
			}
			else
			{
				USB_DISP_Display_NoInfo();
			}
			break;
		case ID3TAG_BITRATE:
			if  (USB_GetTextStatus(UI_TEXT_READY_BITRATE))
			{
				usBitrate = sUSB_PlayInfo.BitRate;			
				ucBuf[0] = (usBitrate / 100) + '0';
				usBitrate = (usBitrate % 100);
				ucBuf[1] = (usBitrate / 10) + '0';
				usBitrate = usBitrate % 10;
				ucBuf[2] = usBitrate + '0';
			#ifdef  ST7565P
				ucBuf[3] = ' ';
				ucBuf[4] = 'k';
				ucBuf[5] = 'b';
				ucBuf[6] = 'p';
				ucBuf[7] = 's';
				ucBuf[8] = 0x00;
			#endif
			#ifdef  PT6524
				ucBuf[3] = ' ';
				ucBuf[4] = 'K';
				ucBuf[5] = 'B';
				ucBuf[6] = 'P';
				ucBuf[7] = 'S';
				ucBuf[8] = 0x00;
			#endif
			#ifdef  ST7565P
				ST7565_UpdateDisplayString(7, &ucBuf[0], 0);
			#endif
			#ifdef  PT6524
				memcpy(&ucDISP_Buf[3], &ucBuf[0], 8);
			#endif
			}
			else
			{
				USB_DISP_Display_NoInfo();
			}
	}
}

void USB_DISP_SubText(void)
{
	unsigned char ucBuf[3];
	memcpy(&ucBuf[0], "USB", 3);
		
#ifdef  ST7565P
	ST7565_UpdateSubDisplayData(&ucBuf[0]);	
#endif
#ifdef  PT6524
	memcpy(&ucDISP_Buf[0],&ucBuf[0], 3);	
#endif
}

void USB_DISP_MainText(void)
{
	LC786X_MEDIA_STATUS	USB_Status;

	USB_Status = LC786XXX_GetMediaStatus(FUNC_USB);
#ifdef  MIC
	if	(MIC_GetMicStatus() == MIC_ON)
	{
		if  ((ucUSB_Pause == 0) && (LC786XXX_GetMediaStatus(FUNC_USB) != LC786X_MS_PAUSE))
		{
			MIC_DISP_MainText();
			return;
		}
	}
#endif
	if  ((ucLC786XXX_InitializeComplete == 0) || (usLC786XXX_InitUSB_10mecTimer != 0))
	{
		USB_DISP_FileCheck();
		return;
	}
		
	if	(ucLC786XXX_ErrorSeq != 0)
	{
		DISP_ErrorCode();
		return;
	}
	if  ((ucUSB_Pause == 1) || (USB_Status == LC786X_MS_PAUSE))
	{
		USB_DISP_Pause();
	}
	else  if  ((USB_Status == LC786X_MS_FASTFORWARD) 
				|| (USB_Status == LC786X_MS_FASTBACK)
				|| (ucUSB_FastPlay != MEDIA_NORMAL_PLAY))
	{
		USB_DISP_FastF_FastR();
	}
	else  if  (
				//(LC786XXX_GetMediaStatus(FUNC_USB) == LC786X_MS_DEVICE_CONNECT) 
				(USB_Status == LC786X_MS_MASS_STORAGE_CONNECT)
				|| (USB_Status == LC786X_MS_MASS_STORAGE_CHECK) 
				|| (USB_Status == LC786X_MS_CONTENTS_ANALYSIS) 
				|| (USB_Status == LC786X_MS_PAUSE_AT_STARTUP) 
				|| (USB_Status == LC786X_MS_STOP_NO_TOC)
				)
	{
		USB_DISP_FileCheck();
	}
	else  
	{
		if  (ucDISP_FolderName == 1)
		{
			if  (USB_DISP_FolderName() == 1)
			{
				return;
			}
		}	
		if  (ucDISP_FileName == 1)
		{
			if  (USB_DISP_FileName() == 1)
			{
				return;
			}
		}
		
		if  ((ucDISP_ClockMode == 1) && (ucDISP_CurrentDisplay_Timer50msec == 0))
		{
			DISP_MainText_Clock();
			return;
		}
		
		switch  (ucUSB_DisplayInfo)
		{  
			case INFO_FILENAME:
				USB_DISP_FileName();
				break;
			case INFO_PLAYTIME:
				USB_DISP_PlayTime();
				break;
			case INFO_FOLDERNAME:
				USB_DISP_FolderName();
				break;
			case INFO_ID3TAG:
				UI_USB_DISP_ID3Tag();
				break;
		}
	}	
}


